package vault

import "database/sql"

//region TableMap

type TableMap struct {
	PageTitle string
	Headers   []TableHeader
	Rows      []TableRow
}

func NewTableMap(header []TableHeader, rows []TableRow) *TableMap {
	return &TableMap{
		Headers: header,
		Rows:    rows,
	}
}

func (tm *TableMap) UpdateHeaderByKey(key, newValue string) {
	for i := range tm.Headers {
		if tm.Headers[i].Key == key {
			tm.Headers[i].Value = newValue
			return
		}
	}
}

type TableHeader struct {
	Key       string
	Value     any
	IsShow    bool
	IsExport  bool
	FieldTmpl string
}

func NewTableHeader(item *sql.ColumnType) *TableHeader {
	var tmpl = ""
	if item.DatabaseTypeName() == "DATE" {
		tmpl = `function ` + item.Name() + `Templet(d) {
            return layui.util.toDateString(d.` + item.Name() + `, 'yyyy-MM-dd')
        }`
	}

	x := &TableHeader{
		Key:       item.Name(),
		Value:     item.Name(),
		IsShow:    true,
		IsExport:  true,
		FieldTmpl: tmpl,
	}

	return x
}

type TableRow map[string]any

//endregion
